﻿//----------------------------------------------------------------------
// <copyright file="CacheBaseMaServerInfo.cs" company="NTT DATA BEEN (China)">
// Copyright (c)2012 NTT DATA BEEN (China) Information Technology Co.,Ltd.
// </copyright>
// <author> lidawei </author>
// <summary>
// システム名称　　：
// モジュールＩＤ　：
// モジュール名称　：
// 機能概要　　　　：CacheBaseMaServerInfo类 
// 改訂履歴　　　　：
// 2012-08-01 新規 lidawei    Been0001   
// </summary>
//---------------------------------------------------------------------- 

namespace Ntt.NttData.Been.Management.SolutionA.Sync.Operator.TargetDAL
{
	using System;
	using System.Collections.Generic;
	using System.Data;
	using System.Data.SqlClient;
	using CommonLibrary.Database.SqlServer;
	using Common.Data;
	using Model;
	using BLL;

	public class Ma19Database : DatabaseOperation
	{
		#region sqlstring
		private const string  SELECTSQL = @" SELECT  [f_operator_no] "+
														" ,[f_operator_pass] "+
														" ,[f_operator_name_kana] "+
														" ,[f_operator_name] "+
														" ,[JobLevel] "+
														" ,[ERankID] "+
														" ,[f_operator_update_date] " +
													" FROM  [t_im_operator_mas]";

		private const string INSERTSQL = @"INSERT INTO [t_im_operator_mas] " +
													" ( [f_operator_no] " +
													"   ,[f_operator_pass] " +
													"   ,[f_operator_name_kana] " +
													"   ,[f_operator_name] " +
													"   ,[JobLevel] " +
													"   ,[ERankID] " +
													"   ,[f_operator_update_date]) " +
													" VALUES " +
													"   (@OperatorNo " +
													"   ,@OperatorPass " +
													"   ,@OperatorNameKana " +
													"   ,@OperatorName " +
													"   ,@OperatorGender " +
													"   ,@ERankId,@UpdateTime) ";


		private const string UPDATESQL = @"UPDATE [t_im_operator_mas] " +
													"   SET	[f_operator_pass]	= @OperatorPass " +
													"   	,[f_operator_name_kana]	=	@OperatorNameKana " +
													"   	,[f_operator_name]	=	@OperatorName " +
													"   	,[JobLevel]	=	@OperatorGender " +
													"   	,[ERankID]	=	@ERankId " +
													"   	,[f_operator_update_date]	=	@UpdateTime " +
													"   WHERE [f_operator_no] = @OperatorNo ";

		private const string DELETESQL = @"DELETE FROM [t_im_operator_mas] WHERE [f_operator_no] = @OperatorNo";
		#endregion

		/// <summary>
		/// Prevents a default instance of the <see cref="Ma19Database"/> class from being created.
		/// </summary>
		/// <param name="maAddress">The ma address.</param>
		private Ma19Database(string maAddress):base(maAddress){}
		
		/// <summary>
		/// Gets the operators rank.
		/// </summary>
		/// <param name="maAddress">The ma address.</param>
		/// <returns></returns>
		public static IEnumerable<EntityOperator> GetOperatorsRank(string maAddress)
		{
			List<EntityOperator> entityOperators ;
			try
			{
				using (var target = new Ma19Database(maAddress))
				{
					entityOperators = target.SelectOperatorsRank();
					
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return entityOperators;
		}
		
		/// <summary>
		/// Adds the operators.
		/// </summary>
		/// <param name="maAddress">The ma address.</param>
		/// <param name="entityOperator">The entity operator.</param>
		/// <returns></returns>
		public static int AddOperators(string maAddress,EntityOperator entityOperator)
		{
			int effactRows ;
			try
			{
				using (var target = new Ma19Database(maAddress))
				{
					target.BeginTransaction();
					effactRows = target.InsertOperator(entityOperator);
					target.CommitTransaction();
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return effactRows;
		}

		/// <summary>
		/// Sets the operators.
		/// </summary>
		/// <param name="maAddress">The ma address.</param>
		/// <param name="entityOperator">The entity operator.</param>
		/// <returns></returns>
		public static int SetOperators(string maAddress, EntityOperator entityOperator)
		{
			int effactRows ;
			try
			{
				using (var target = new Ma19Database(maAddress))
				{
					target.BeginTransaction();
					effactRows = target.UpdateOperator(entityOperator);
					target.CommitTransaction();
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return effactRows;
		}

		/// <summary>
		/// Removes the operators.
		/// </summary>
		/// <param name="maAddress">The ma address.</param>
		/// <param name="operatorNo">The operator no.</param>
		/// <returns></returns>
		public static int RemoveOperators(string maAddress, string operatorNo)
		{
			int effactRows ;
			try
			{
				using (var target = new Ma19Database(maAddress))
				{
					target.BeginTransaction();
					effactRows = target.DeleteOperator(operatorNo);
					target.CommitTransaction();
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return effactRows;
		}
		
		/// <summary>
		/// Selects the operators rank.
		/// </summary>
		/// <returns></returns>
		private List<EntityOperator> SelectOperatorsRank()
		{
			List<EntityOperator> entityOperators = null;
			try
			{
					if (sqlConnection != null)
					{


						using (SqlDataReader reader = new SqlHelper().ExecuteReader(sqlConnection, CommandType.Text, SELECTSQL))
						{

							while (reader.Read())
							{

								var item = new EntityOperator
								{
									OperatorNo = reader.GetStringValue("f_operator_no"),
									OperatorPass = reader.GetStringValue("f_operator_pass"),
									OperatorNameKana = reader.GetStringValue("f_operator_name"),
									OperatorName = reader.GetStringValue("f_operator_name"),
									OperatorGender = reader.GetInt32Value("JobLevel"),
									ERankId = reader.GetInt32Value("ERankID"),
									UpdateTime = reader.GetDateTimeValue("f_operator_update_date")
								  
								};

								if (entityOperators == null)
								{
									entityOperators = new List<EntityOperator>();
								}

								entityOperators.Add(item);
							}

							reader.Close();
						}
					}
			}
			catch (Exception ex)
			{
				throw;
			}

			return entityOperators;
		}

		/// <summary>
		/// Inserts the operator.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <returns></returns>
		private int InsertOperator(EntityOperator entity)
		{
			int recordsAffected = 0;
			try
			{
				if (sqlConnection != null)
				{
					var parameters = GetParameters(entity);

					recordsAffected = new SqlHelper().ExecuteNonQuery(sqlTransaction, CommandType.Text, INSERTSQL, parameters);
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return recordsAffected;
		}
		
		/// <summary>
		/// Updates the operator.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <returns></returns>
		private int UpdateOperator(EntityOperator entity)
		{
			int recordsAffected = 0;
			try
			{
				if (sqlConnection != null)
				{
					var parameters = GetParameters(entity);

					recordsAffected = new SqlHelper().ExecuteNonQuery(sqlTransaction, CommandType.Text, UPDATESQL, parameters);
				}
			}
			catch (Exception ex)
			{
				throw;
			}
			return recordsAffected;
		}

		/// <summary>
		/// Deletes the operator.
		/// </summary>
		/// <param name="operatorNo">The operator no.</param>
		/// <returns></returns>
		private int DeleteOperator(string operatorNo)
		{
			int recordsAffected = 0;
			try
			{
				if (sqlConnection != null)
				{
					SqlParameter[] parameters =
				{
					new SqlParameter("@OperatorNo",operatorNo)
				   
				};

					recordsAffected = new SqlHelper().ExecuteNonQuery(sqlTransaction, CommandType.Text, DELETESQL, parameters);
				}
			}
			catch (Exception ex)
			{
				throw ;
			}
			return recordsAffected;
		}
		
		/// <summary>
		/// Gets the parameters.
		/// </summary>
		/// <param name="entity">The entity.</param>
		/// <returns></returns>
		private SqlParameter[] GetParameters(EntityOperator entity)
		{
			SqlParameter[] parameters =
				{
					new SqlParameter("@OperatorNo", entity.OperatorNo),
					new SqlParameter("@OperatorPass", entity.OperatorPass),
					new SqlParameter("@OperatorName", entity.OperatorName),
					new SqlParameter("@OperatorNameKana", entity.OperatorNameKana),
					new SqlParameter("@OperatorGender", entity.OperatorGender),
					new SqlParameter("@ERankId", entity.ERankId),
					new SqlParameter("@UpDatetime", entity.UpdateTime)
				};
			return parameters;
		}
	}
}
